<?php
require_once 'database21.php';

class Teaching {
    private $db;
    
    public function __construct() {
        $this->db = new Database();
    }
    
    // 获取所有授课安排
    public function getTeachings() {
        $sql = "SELECT t.teaching_id, t.teacher_id, t.course_id, t.semester,
                       te.teacher_name, c.course_name
                FROM teaching t 
                JOIN teachers te ON t.teacher_id = te.teacher_id 
                JOIN courses c ON t.course_id = c.course_id
                ORDER BY t.teaching_id";
        return $this->db->query($sql);
    }
    
    // 获取单个授课安排
    public function getTeaching($id) {
        $sql = "SELECT * FROM teaching WHERE teaching_id = ?";
        $result = $this->db->query($sql, [$id]);
        return $result ? $result[0] : null;
    }
    
    // 检查授课安排是否已存在
    public function checkTeachingExists($teacher_id, $course_id, $semester) {
        $sql = "SELECT COUNT(*) as count FROM teaching 
                WHERE teacher_id = ? AND course_id = ? AND semester = ?";
        $result = $this->db->query($sql, [$teacher_id, $course_id, $semester]);
        return $result[0]['count'] > 0;
    }
    
    // 检查授课安排是否已存在（排除当前记录）
    public function checkTeachingExistsExcept($teacher_id, $course_id, $semester, $teaching_id) {
        $sql = "SELECT COUNT(*) as count FROM teaching 
                WHERE teacher_id = ? AND course_id = ? AND semester = ? AND teaching_id != ?";
        $result = $this->db->query($sql, [$teacher_id, $course_id, $semester, $teaching_id]);
        return $result[0]['count'] > 0;
    }
    
    // 创建授课安排
    public function createTeaching($teacher_id, $course_id, $semester) {
        $sql = "INSERT INTO teaching (teacher_id, course_id, semester) VALUES (?, ?, ?)";
        return $this->db->execute($sql, [$teacher_id, $course_id, $semester]);
    }
    
    // 更新授课安排
    public function updateTeaching($id, $teacher_id, $course_id, $semester) {
        $sql = "UPDATE teaching SET teacher_id = ?, course_id = ?, semester = ? WHERE teaching_id = ?";
        return $this->db->execute($sql, [$teacher_id, $course_id, $semester, $id]);
    }
    
    // 删除授课安排
    public function deleteTeaching($id) {
        $sql = "DELETE FROM teaching WHERE teaching_id = ?";
        return $this->db->execute($sql, [$id]);
    }
    
    // 获取所有教师
    public function getTeachers() {
        $sql = "SELECT t.teacher_id, t.teacher_name, d.department_name 
                FROM teachers t 
                LEFT JOIN departments d ON t.department_id = d.department_id 
                ORDER BY t.teacher_name";
        return $this->db->query($sql);
    }
    
    // 获取所有课程
    public function getCourses() {
        $sql = "SELECT c.course_id, c.course_name, c.credit 
                FROM courses c 
                ORDER BY c.course_name";
        return $this->db->query($sql);
    }
    
    // 获取所有班级
    public function getClasses() {
        $sql = "SELECT c.class_id, c.class_name, d.department_name 
                FROM classes c 
                LEFT JOIN departments d ON c.department_id = d.department_id 
                ORDER BY c.class_name";
        return $this->db->query($sql);
    }
    
    // 获取教师的所有授课安排
    public function getTeacherTeachings($teacher_id) {
        $sql = "SELECT t.teaching_id, t.semester, c.course_name
                FROM teaching t 
                JOIN courses c ON t.course_id = c.course_id 
                WHERE t.teacher_id = ?
                ORDER BY t.semester DESC, c.course_name";
        return $this->db->query($sql, [$teacher_id]);
    }
    
    // 获取班级的所有授课安排
    // 注意：由于teaching表中移除了class_id字段，此方法可能需要重新设计
    // 如果需要按班级查询授课安排，可能需要通过其他表的关联来实现
    public function getClassTeachings($class_id) {
        // 此方法需要重新设计，因为teaching表中不再有class_id字段
        return [];
    }
}
?>